package day7.sp;


import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class Market  implements Serializable {
    private String name;
    private String address;

    List<Member> list = new ArrayList();

    public static final String OBJFILE = "market.txt";

//    @Override
//    public void run() {
//        while (true){
//            try {
//                sleep(5000);
//                storeObj(this);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//    }

    public static Market readFromFile() {
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(OBJFILE));
            Market market = (Market) objectInputStream.readObject();
            objectInputStream.close();
            System.out.println("对象已经存在！");
            return market;
        } catch (Exception e) {
            //e.printStackTrace();

        }
        System.out.println("对象不存在");
        return null;
    }

    public static void storeObj(Market m) {
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(OBJFILE));
            objectOutputStream.writeObject(m);
            objectOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void xvliehua() {
//        try {
//            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("chapter04.txt"));
//            list  = (List<Member>) objectInputStream.readObject();
//            objectInputStream.close();
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }

    }


    public void addk(String cardNumber, String name, String phone, String password) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formater = new SimpleDateFormat("yyyy- MM-dd HH:mm:ss");
        String now = formater.format(calendar.getTime());
        boolean a = false;
        for (Member member1 : list) {
            if (member1.getCardNumber().equals(cardNumber)) {
                a = true;
                System.out.println("工号重复，添加失败!");
                break;
            }
        }
        if (a == false) {
            list.add(new Member(cardNumber, name, phone, 0, password, now));
            System.out.println("开卡成功！");
        }


//        try {
//            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:\\aaa\\chapter04.txt"));
//            objectOutputStream.writeObject(list);
//            objectOutputStream.close();
//        }catch (Exception e){
//            e.printStackTrace();
//        }

    }

    public void inof() {
        //
        System.out.println(name + "位于" + this.address);
        for (Member member : list) {
            System.out.println(member);
        }
    }

    public void addIntegral(String cardNumber, int integral) {
        //添加积分
        for (Member member : list) {
            if (member.getCardNumber().equals(cardNumber)) {
                member.setIntegral(member.getIntegral() + integral);
                System.out.println(member.getName() + "添加之后的积分为：" + member.getIntegral());
            }
        }
    }

    public void cashPrize(Map<String, Integer> map, String cardNumber, String password) {
        Scanner in = new Scanner(System.in);

        boolean tab = false;
        for (Member member : list) {
            if (member.getCardNumber().equals(cardNumber) && member.getPassword().equals(password)) {
                System.out.println("可以兑换一下奖品：");
                System.out.println("1：300分兑抽纸\n" + "2：1000分兑血压计\n" + "3：10000兑iphone6");
                System.out.println("请输入你要兑换奖品的序号：");
                int n = in.nextInt();
                if (n == 1) {
                    if (member.getIntegral() >= 300) {
                        for (Map.Entry entry : map.entrySet()) {
                            if (entry.getKey().equals("抽纸")) {
                                Integer a = (int) entry.getValue();
                                map.put("抽纸", a - 1);
                                System.out.println(entry.getKey() + "=" + entry.getValue());
                                tab = true;
                            }
                        }
                        member.setIntegral(member.getIntegral() - 300);
                        System.out.print("抽纸，");
                    } else {
                        System.out.println("积分不够");
                    }
                } else if (n == 2) {
                    if (member.getIntegral() >= 1000) {
                        for (Map.Entry entry : map.entrySet()) {
                            if (entry.getKey().equals("血压计")) {
                                Integer a = (int) entry.getValue();
                                map.put("血压计", a - 1);
                                System.out.println(entry.getKey() + "=" + entry.getValue());
                            }
                        }
                        tab = true;
                        member.setIntegral(member.getIntegral() - 1000);
                        System.out.print("血压计，");
                    } else {
                        System.out.println("积分不够");
                    }
                } else if (n == 3) {
                    if (member.getIntegral() >= 10000) {
                        for (Map.Entry entry : map.entrySet()) {
                            if (entry.getKey().equals("iphone6")) {
                                Integer a = (int) entry.getValue();
                                map.put("iphone6", a - 1);
                                System.out.println(entry.getKey() + "个数为" + entry.getValue());
                            }
                        }

                        tab = true;
                        member.setIntegral(member.getIntegral() - 10000);
                        System.out.print("iphone6，");
                    } else {
                        System.out.println("积分不够");
                    }
                } else {
                    System.out.println("选择错误");
                }

            }
        }
        if (tab) {
            System.out.println("兑换成功！");
        } else {
            System.out.println("卡号或者密码不正确！");
        }


    }

    public void queryIntegral(String cardNumber) {
        //查询积分
        for (Member member : list) {
            if (member.getCardNumber().equals(cardNumber)) {
                System.out.println(member.getName() + "的积分为：" + member.getIntegral());
            }
        }
    }


    public void changePassword(String cardNumber, String password) {
        //修改密码
        Scanner in = new Scanner(System.in);
        boolean tab = false;
        for (Member member : list) {
            if (member.getCardNumber().equals(cardNumber) && member.getPassword().equals(password)) {
                System.out.println("请输入你要修改后的密码:");
                String a = in.next();
                member.setPassword(a);
                tab = true;
            }
        }
        if (tab) {
            System.out.println("修改成功！");
        } else {
            System.out.println("卡号或者密码不正确！");
        }
    }

    public void delete(String num) {
        for (Member member : list) {
            if (member.getCardNumber().equals(num)) {
                list.remove(member);
                return;
            }
        }
        System.out.println("会员号输入错误");
    }
//    public void end(){
//        Calendar calendar = Calendar.getInstance();
//        SimpleDateFormat formater = new SimpleDateFormat("yyyy- MM-dd HH:mm:ss");
//        try {
//            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:\\aaa\\chapter04.txt"));
//            objectOutputStream.writeObject(list);
//            objectOutputStream.close();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
}
